//引入封装后的axios
import apis from '../../http/apis/apis.js';

export default {
  namespaced: true,
  state: {
    typesName: '',
    types: [],
    //分页   totals:总共页数，tableData：获取的数据，pageSize：页面渲染的数据
    totals:0,
    tableData:[],
    pageSize:3,
    currentPage:1,
    select:{
      input: '',
      selectValue:"typesName",
  }

  },
  mutations: {
    //接收setTypes的值,赋值给setTypes
    //保险产品分类
    settypesName(state, data) {
      state.typesName = data
    },
    setTypes(state, data) {
      state.totals=data.total
      state.tableData=data.types
      state.types = data.types
    },
    setSearchTypes(state, data){
      state.totals=data.total,
      state.tableData = data.types
      state.types = data.types
    },
    setpageSize(state,pageSize){
      state.pageSize=pageSize
    },
    setcurrentPage(state,currentPage){
      state.currentPage=currentPage
    }
  },
  //方法，类似于组件中的methods
  // actions，用来设置异步方法
  actions: {
    //context参数是不可改变的，他接收到的是仓库对象
    // 添加产品分类
    async addTypes(context, typesName) {
      const result = await apis.types.addTypes(typesName)
      return result
    },

    //获取产品分类信息
    async getTypes(context) {
      const data = await apis.types.getTypes({
        pageSize:context.state.pageSize,
        currentPage:context.state.currentPage
      })
      if (data.code) {
        context.commit('setTypes', data.data)
      }
    },

    //获取点击删除时，产品分类下是否还有数据
    async getTypesId(context, _id) {
      const data = await apis.types.getTypesId({_id:_id})
      return data
    },

    //根据ID删除指定产品分类
    async deleteTypesId(context, _id) {
      const data = await apis.types.deleteTypesId(_id)
      return data
    },

    //修改
    async modifyTypes(context, _id) {
      const data = await apis.types.modifyTypes(_id)
      return data
    },
    ////搜索
    async searchTypes(context,search) {
      const data = await apis.types.searchTypes({
        pageSize:context.state.pageSize,
        currentPage:context.state.currentPage,
        input:search.input,
        selectValue:search.selectValue
      })
      if (data.code) {
        context.commit('setSearchTypes', data.types)
      }
      return data
    }

  },

}